#!/bin/bash
User=nginx
SourceDir=/usr/local/src
InstallDir=/apps/nginx

function CreateUser() {
    if id ${User} &> /dev/null ;then
        echo "${User} 用户已存在"
    else
        useradd -s /bin/bash -m -U ${User}
        echo "${User} 用户创建成功"
    fi
}

function InstallPackage() {
    apt update
    apt install -y wget curl gcc make libgd-dev libgeoip-dev libpcre3 libpcre3-dev openssl libssl-dev libxml2-dev libxml2 libxslt-dev libperl-dev
}

function InstallTengine() {
    cd ${SourceDir}
    wget https://tengine.taobao.org/download/tengine-2.4.1.tar.gz
    tar xf tengine-2.4.1.tar.gz
    cd tengine-2.4.1
    ./configure  --prefix=${InstallDir} \
    --user=${User} \
    --group=${User} \
    --with-ipv6 \
    --with-select_module \
    --with-poll_module \
    --with-threads \
    --with-file-aio \
    --with-http_ssl_module \
    --with-http_v2_module \
    --with-http_realip_module \
    --with-http_addition_module \
    --with-http_xslt_module \
    --with-http_image_filter_module \
    --with-http_xslt_module=dynamic \
    --with-http_image_filter_module=dynamic \
    --with-debug \
    --with-compat \
    --with-http_auth_request_module \
    --with-http_dav_module \
    --with-http_degradation_module \
    --with-http_flv_module \
    --with-http_gunzip_module \
    --with-http_gzip_static_module \
    --with-http_mp4_module \
    --with-http_perl_module=dynamic \
    --with-http_random_index_module \
    --with-http_secure_link_module \
    --with-http_slice_module \
    --with-http_stub_status_module \
    --with-http_sub_module \
    --with-mail=dynamic \
    --with-mail_ssl_module \
    --with-pcre \
    --with-pcre-jit \
    --with-stream=dynamic \
    --with-stream_ssl_module \
    --with-stream_ssl_preread_module

    make && make install
}

function SetPATH() {
    echo 'PATH=${InstallDir}/sbin$PATH' >> /etc/profile.d/nginx.sh
    source /etc/profile.d/nginx.sh
}

function MakeDir() {
    mkdir -p ${InstallDir}/conf/conf.d
}

function MakeConfig() {
    mv ${InstallDir}/conf/nginx.conf ${InstallDir}/conf/nginx.conf.backup
echo "
user  User User;
worker_processes  auto;
worker_cpu_affinity auto;
worker_rlimit_nofile 65535;
events {
    worker_connections  65535;
    #reuse_port on; ###linux kernel 3.9 SO_REUSEPORT
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    server_tag off;
    server_info off;
    proxy_set_header        Host \$http_host;
    proxy_set_header        X-Real-IP \$remote_addr;
    proxy_set_header        X-Forwarded-For \$proxy_add_x_forwarded_for;
    #proxy_set_header        X-Forwarded-Proto \$scheme;
    client_max_body_size    300m;
    client_body_buffer_size 1m;
    proxy_connect_timeout   60;
    proxy_send_timeout      300;
    proxy_read_timeout      300;
    proxy_buffer_size       32k;
    proxy_buffers           4 64k;
    proxy_busy_buffers_size 128k;
    proxy_temp_file_write_size 128k;
    fastcgi_intercept_errors on;
    proxy_intercept_errors on;
    log_format  main  '\$remote_addr - \$remote_user [\$time_local] "\$request" '
                      '\$status \$body_bytes_sent "\$http_referer" '
                      '"\$http_user_agent" "\$http_x_forwarded_for" "\$host" \$upstream_addr'
                      '"\$request_time" "\$upstream_response_time" "\$pipe"';

    sendfile        on;
    open_log_file_cache max=1000 inactive=20s;
    gzip  on;
    gzip_comp_level 4;
    gzip_min_length  2k;
    gzip_proxied any;
    gzip_vary on;
    gzip_http_version 1.0;
    gzip_disable "MSIE [1-6].";
    gzip_types text/plain application/x-javascript text/css text/javascript image/jpeg image/gif image/png;
    proxy_cache_path /tmp/nginx/ levels=1:2 keys_zone=anjian:200m inactive=1h max_size=2G;
    include conf.d/*.conf;
    include conf.d/*/*.conf;
    #keepalive_timeout  0;
}" > ${InstallDir}/conf/nginx.conf
sed -i "s/User/${User}/g" /apps/nginx/conf/nginx.conf
}

function ChangeUser() {
    chown -R ${User}: ${InstallDir}
}

function MakeService() {
cat >> /lib/systemd/system/nginx.service <<EOF
[Unit]
Description=A high performance web server and a reverse proxy server
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=${InstallDir}/logs/nginx.pid
ExecStart=${InstallDir}/sbin/nginx -g 'daemon on; master_process on;' -c ${InstallDir}/conf/nginx.conf
ExecReload=${InstallDir}/sbin/nginx -g 'daemon on; master_process on;' -s reload
ExecStop=/bin/kill -s TERM \$MAINPID
TimeoutStopSec=5
KillMode=mixed

[Install]
WantedBy=multi-user.target
EOF

}

function StartTengine() {
    systemctl daemon-reload
    systemctl start nginx
    systemctl enable nginx
    if [ ! -z "$(ps -ef | grep nginx | grep -v grep)" ]; then
        echo "nginx install success"
    else
        echo "nginx install fail"
    fi
}

function Main(){
    CreateUser
    InstallPackage
    InstallTengine
    SetPATH
    MakeDir
    MakeConfig
    ChangeUser
    MakeService
    StartTengine
}

Main